﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;
using CE.Global;
using CE.Provider;


public partial class Provider_CarInsurance_ProviderCarPromotion : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICOINTRO";
    public static Int64 _providercarpromoid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            ClearProcess();
            if (Request.Params["PRID"] != null)
            {
                Int32 _CarPromoid;
                if (Int32.TryParse(Request.Params["PRID"].ToString(), out _CarPromoid))
                {
                    ShowCarPromo(_CarPromoid);
                }
            }
        }
    }

    private void ShowCarPromo(Int64 ProviderCarPromoId)
    {
        CarPromotion _CarPromo = new CarPromotion();
        CarPromotionDB _CarPromodb = new CarPromotionDB();

        _CarPromo = _CarPromodb.GetProviderCarPromotion(ProviderCarPromoId);

        if (_CarPromo != null)
        {
            _providercarpromoid = Convert.ToInt64(_CarPromo.PromotionId.ToString());
            lblProviderCarPromoId.Text = _CarPromo.PromotionId.ToString();
            txtPromoTitle.Text = _CarPromo.PromotionTitle;
            txtPromoDescription.Text = _CarPromo.PromotionDescription;
            chkStatus.Checked = Convert.ToBoolean(_CarPromo.Status);

        }
    }

    public void ClearProcess()
    {
        _providercarpromoid = 0;
        lblProviderCarPromoId.Text = "0";
        txtPromoTitle.Text = "";
        txtPromoDescription.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";

    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarPromotion _CarPromo = new CarPromotion();
                CarPromotionDB _CarPromodb = new CarPromotionDB();

                int _trantype = 1;
                _CarPromo.PromotionId = Convert.ToInt64(lblProviderCarPromoId.Text);
                if (_CarPromo.PromotionId > 0)
                {
                    _trantype = 2;
                }

                _CarPromo.PromotionTitle = txtPromoTitle.Text.Trim();
                _CarPromo.PromotionDescription = txtPromoDescription.Text.Trim();
                _CarPromo.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _CarPromo.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _CarPromo.Status = Convert.ToBoolean(chkStatus.Checked.ToString());
                _CarPromo.PromotionId = _CarPromodb.Insert(_CarPromo);

                if (_CarPromo.PromotionId > 0)
                {
                    ClearProcess();
                    InsertProviderAuthTransaction(_CarPromo.PromotionId, "CICarPromotion", _trantype, 100, _authcode, _CarPromo.ProviderCompanyId, _CarPromo.RefCEUserId);
                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderCarPromoId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {

                CarPromotionDB _CarPromodb = new CarPromotionDB();
                if (_CarPromodb.Delete(Convert.ToInt64(lblProviderCarPromoId.Text), _providercompanyid))
                    {
                        InsertProviderAuthTransaction(Convert.ToInt64(lblProviderCarPromoId.Text), "CICarPromotion", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderCarPromotion.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
               
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }

    }

    protected void ibtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCarPromotion.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
